/**
 * optType 1创建模块 4创建分组 7创建标签
 *         2修改模块 5修改分组 8修改标签
 *         3删除模块 6删除分组 9删除标签
 * */
var optType = 1, optModuleIndex = 0, optModuleRow = null,
    optGroupIndex = 0, optGroupRow = null,
    optLabelIndex = 0, optLabelRow = null,
    base = {type:1}, dictData = [];
var label = {
    module: {
        title: '',
        height: kit.wh().height - 40,
        singleSelect: true,
        pagination: false,
        onClickRow:function () {
            optModuleRow = component.datagrid.getRowData('#table-module');
            optModuleIndex = component.datagrid.getDataRow('#table-module');
            $('#panel-group').panel({title: '[' +optModuleRow.name + '] 标签分组'})
            action.getGroup(optModuleRow.id);
            optLabelRow = null;
            component.datagrid.load('#table-label', []);
            $('#panel-label').panel({title: '标签数据'})
        },
        frozenColumns:[
            [
                {field:'name',title:'模块名称',align:'center'},
                {field:'code',title:'模块编码',align:'center'},
            ]
        ],
        columns:[
            [
                {field:'sort',title:'排序',align:'center'},
                {field:'remark',title:'备注',align:'left'}
            ]
        ]
    },
    group:{
        title: '',
        height: kit.wh().height - 40,
        singleSelect: true,
        pagination: false,
        onClickRow:function () {
            optGroupRow = component.datagrid.getRowData('#table-group');
            optGroupIndex = component.datagrid.getDataRow('#table-group');
            $('#panel-label').panel({title: '[' +optGroupRow.name + '] 标签数据'})
            action.getLabel(optGroupRow.id);
        },
        frozenColumns:[
            [
                {field:'name',title:'模块名称',align:'center'},
                {field:'code',title:'模块编码',align:'center'},
            ]
        ],
        columns:[
            [
                {field:'sort',title:'排序',align:'center'},
                {field:'remark',title:'备注',align:'left'}
            ]
        ]
    },
    base: {
        title: '',
        height: kit.wh().height - 40,
        singleSelect: true,
        pagination: false,
        onClickRow:function () {
            optLabelRow = component.datagrid.getRowData('#table-label');
            optLabelIndex = component.datagrid.getDataRow('#table-label');
        },
        frozenColumns:[
            [
                {field:'name',title:'模块名称',align:'center'},
                {field:'code',title:'模块编码',align:'center', width:150},
            ]
        ],
        columns:[
            [
                {field:'type',title:'数据类型',align:'center',
                    formatter:function (val, row, index) {
                        switch (val){
                            case 1:
                                return '字符串';
                                break;
                            case 2:
                                return '数值型';
                                break;
                            case 3:
                                return '日期';
                                break;
                            case 9:
                                return '选项(字典)';
                                break;
                        }
                    }
                },
                {field:'remark',title:'备注',align:'left'},
                {field:'sort',title:'排序',align:'center'}
            ]
        ]
    },
    form:{
        title: '标签操作',
        width:400,
        height:300,
        buttons: [{
            text:'保 存',
            iconCls:'icon-accept icon16',
            handler:function(){
                switch (optType){
                    case 1:
                        action.add('#table-module');
                        break;
                    case 2:
                        action.edit(optModuleRow.id, optModuleIndex, '#table-module');
                        break;

                    case 4:
                        action.add('#table-group');
                        break;
                    case 5:
                        action.edit(optGroupRow.id, optGroupIndex, '#table-group');
                        break;

                    case 7:
                        action.add('#table-label');
                        break;
                    case 8:
                        action.edit(optLabelRow.id, optLabelIndex, '#table-label');
                        break;
                }
            }
        },{
            iconCls:'icon-cancel icon16',
            text:'关 闭',
            handler:function(){
                component.dialog.close('#opt-form');
            }
        }]
    }
}

$(function () {
    component.datagrid.init('#table-group', label.group, false);
    component.datagrid.init('#table-label', label.base, false);
    action.getModule();
})

var opt = {
    addModule:function (_event) {
        kit.form.clean('#opt-form-data');
        label.form.title = '创建模块';
        label.form.height = 300;
        component.dialog.init('#opt-form', label.form);
        component.dialog.open('#opt-form');
        kit.form.set('#opt-form-data', base);
        optType = 1;
        $('#tr-type').hide();
    },
    editModule:function (_event) {
        if(optModuleRow == null){
            kit.alert('请先选择一条标签模块数据', 'warning');
            return false;
        }
        label.form.title = '创建标签模块';
        label.form.height = 300;
        component.dialog.init('#opt-form', label.form);
        component.dialog.open('#opt-form');
        kit.form.set('#opt-form-data', optModuleRow);
        optType = 2;
        $('#tr-type').hide();
    },
    deleteModule:function (_event) {
        if(optModuleRow == null){
            kit.alert('请先选择一条标签模块数据', 'warning');
            return false;
        }
        optType = 3;
        var fun = "action.delete('" + optModuleRow.id + "', " + optModuleIndex + ");";
        var c = {title: '删除模块', message: '确定要删除这个标签模块?', submit: fun }
        kit.confirm(c);
    },
    addGroup:function (_event) {
        if(optModuleRow == null){
            kit.alert('请先选择标签模块', 'warning');
            return false;
        }
        kit.form.clean('#opt-form-data');
        label.form.title = '创建分组';
        label.form.height = 300;
        base['moduleId'] = optModuleRow.id;
        base['parentId'] = optModuleRow.id;
        component.dialog.init('#opt-form', label.form);
        component.dialog.open('#opt-form');
        kit.form.set('#opt-form-data', base);
        optType = 4;
        $('#tr-type').hide();
    },
    editGroup:function (_event) {
        if(optGroupRow == null){
            kit.alert('请先选择一条标签分组数据', 'warning');
            return false;
        }
        label.form.title = '修改分组';
        label.form.height = 300;
        component.dialog.init('#opt-form', label.form);
        component.dialog.open('#opt-form');
        kit.form.set('#opt-form-data', optGroupRow);
        optType = 5;
        $('#tr-type').hide();
    },
    deleteGroup:function (_event) {
        if(optGroupRow == null){
            kit.alert('请先选择一条标签模块数据', 'warning');
            return false;
        }
        optType = 6;
        var fun = "action.delete('" + optGroupRow.id + "', " + optGroupIndex + ");";
        var c = {title: '删除分组', message: '确定要删除这个标签分组？', submit: fun }
        kit.confirm(c);
    },
    addLabel:function (_event) {
        if(optGroupRow == null){
            kit.alert('请先选择标签分组', 'warning');
            return false;
        }
        kit.form.clean('#opt-form-data');
        label.form.title = '创建标签';
        label.form.height = 350;
        base['moduleId'] = optModuleRow.id;
        base['parentId'] = optGroupRow.id;
        component.dialog.init('#opt-form', label.form);
        component.dialog.open('#opt-form');
        kit.form.set('#opt-form-data', base);
        $('#tr-type').show();
        optType = 7;
    },
    editLabel:function (_event) {
        if(optLabelRow == null){
            kit.alert('请先选择一条标签数据', 'warning');
            return false;
        }
        label.form.title = '修改标签';
        label.form.height = 350;
        component.dialog.init('#opt-form', label.form);
        component.dialog.open('#opt-form');
        kit.form.set('#opt-form-data', optLabelRow);
        $('#tr-type').show();
        optType = 8;
    },
    deleteLabel:function (_event) {
        if(optLabelRow == null){
            kit.alert('请先选择一条标签数据', 'warning');
            return false;
        }
        optType = 9;
        var fun = "action.delete('" + optLabelRow.id + "', " + optLabelIndex + ");";
        var c = {title: '删除标签', message: '确定要删除这个标签？', submit: fun }
        kit.confirm(c);
    }
}
var action = {
    getModule:function () {
        var param = {
            url: constant.route.label.module
        }
        var request = http.get(param);
        request.done(function (d) {
            var data = d.result;
            label.module['data'] = data;
            component.datagrid.init('#table-module', label.module, false);
        })
    },
    getGroup:function (_moduleId) {
        var param = {
            url: constant.route.label.group + '/' + _moduleId
        }
        var request = http.get(param);
        request.done(function (d) {
            var data = d.result;
            component.datagrid.load('#table-group', data);
        })
    },
    getLabel:function (_parentId) {
        var param = {
            url: constant.route.label.base + '/' + _parentId
        }
        var request = http.get(param);
        request.done(function (d) {
            var data = d.result;
            component.datagrid.load('#table-label', data);
        })
    },
    add:function (_table) {
        var url = '', data = kit.form.get('#opt-form-data');
        if(optType == 1){
            url = constant.route.label.module;
        }
        if(optType == 4){
            url = constant.route.label.group;
        }
        if(optType == 7){
            url = constant.route.label.base;
        }
        var param = {
            url: url,
            data: data
        }
        var request = http.post(param);
        request.done(function (d) {
            var data = d.result;
            kit.form.clean('#opt-form-data');
            if(optType == 1){
                kit.form.set('#opt-form-data', {moduleId: data.moduleId, parentId: data.parentId});
            }
            if(optType == 4){
                kit.form.set('#opt-form-data', {moduleId: data.moduleId, parentId: data.parentId});
            }
            if(optType == 7){
                kit.form.set('#opt-form-data', {moduleId: data.moduleId, parentId: data.parentId, type: data.type});
            }
            kit.alert(d.message, 'success');
            component.datagrid.append(_table, data);
        })
    },
    edit:function (_id, _index, _table) {
        var url = '', data = kit.form.get('#opt-form-data');
        if(optType == 2){
            url = constant.route.label.module;
        }
        if(optType == 5){
            url = constant.route.label.group;
        }
        if(optType == 8){
            url = constant.route.label.base;
        }
        data['id'] = _id;
        var param = {
            url: url,
            data: data
        }
        var request = http.put(param);
        request.done(function (d) {
            var data = d.result;
            kit.alert(d.message, 'success');
            component.datagrid.updateRow(_table, _index, data);
            component.dialog.close('#opt-form');
        })
    },
    delete:function (_id, _index) {
        var url = '';
        if(optType == 3){
            url = constant.route.label.module + '/' + _id;
        }
        if(optType == 6){
            url = constant.route.label.group + '/' + _id ;
        }
        if(optType == 9){
            url = constant.route.label.base + '/' + _id ;
        }
        var param = {
            url: url
        }
        var request = http.delete(param);
        request.done(function (d) {
            kit.alert(d.message, 'success');
            if(optType == 3){
                component.datagrid.deleteRow('#table-module', _index);
                optModuleRow = null;
                optGroupRow = null;
                optLabelRow = null;
                component.datagrid.load('#table-group', []);
                component.datagrid.load('#table-label', []);
                $('#panel-group').panel({title: '标签分组'})
                $('#panel-label').panel({title: '标签数据'})
            }
            if(optType == 6){
                component.datagrid.deleteRow('#table-group', _index);
                optGroupRow = null;
                optLabelRow = null;
                component.datagrid.load('#table-label', []);
                $('#panel-label').panel({title: '标签数据'})
            }
            if(optType == 9){
                component.datagrid.deleteRow('#table-label', _index);
                optLabelRow = null;
            }
        })
    }
}
